import Core.WindFunctions as WindFunction
import DataProcess
import datetime
import Core.Config as Config
import Core.WindFunctions as Wind
import Core.Gadget as Gadget
import numpy as np
from WindPy import *

def Download_Xueqiu_Follows_WeeklyDiff(database):
    #
    beginDateTime = datetime(2015,1,1)
    endingDateTime = datetime(2019,9,1)
    instruments = database.Find("Instruments", "Stock")
    num = 0
    for instrument in instruments:
        symbol = instrument["Symbol"]
        num += 1
        # print(symbol, num)
        #
        if num < 1495:
            continue
        #
        datetime1 = instrument["DateTime1"]
        datetime2 = instrument["DateTime2"]
        #
        if datetime2 < beginDateTime:
            print(symbol, num, "Delist earlier than 2015")
            continue
        #
        if datetime1 < beginDateTime:
            datetime1 = beginDateTime
        if datetime2 > endingDateTime:
            datetime2 = endingDateTime

        # ---Request Data---
        newDocuments = []
        # ---Params---
        param = ""
        # ---Fields---
        fields = "xq_focusadded"
        data = w.wsd(symbol, fields, datetime1, datetime2, param)
        if data.ErrorCode != 0:
            print(symbol, num, data.Data)
            continue

        count = len(data.Times)
        for i in range(count):
            v = data.Data[0][i]
            dt = data.Times[i]
            # print(data.Times[i], data.Data[0][i])
            if v == None:
                continue
            if np.isnan(v):
                continue
            #
            newDocument = {}
            newDocument["Symbol"] = symbol
            newDocument["Value"] = v
            newDocument["DateTime"] = dt
            newDocument["Date"] = dt
            newDocument["Key2"] = newDocument["Symbol"] + "_" + Gadget.ToDateTimeString(newDocument["Date"])
            newDocuments.append(newDocument)

        print(symbol, num, "#Data", count, "#Valid", len(newDocuments))

        # ---Save to Database---
        database.Upsert_Many("Factor", "Xueqiu_Follows_Diff_Weekly", [], newDocuments)
        a = 0


if __name__ == '__main__':
    #
    config = Config.Config()
    database = config.DataBase("MySQL")
    #
    Wind.w.start()
    #
    Download_Xueqiu_Follows_WeeklyDiff(database)